package pc

import (
	. "chess_platform/internal/common"
	"fmt"
	"github.com/astaxie/beego/orm"
)

//游戏相关的配置

//添加区域游戏
func NewAreaGame(data map[string]interface{}, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChessSysData)
	values, sql := InsertSql(TableAreaGameList, data)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}

	return result.LastInsertId()
}

//更新区域游戏
func UpdateAreaGame(data map[string]interface{}, id int64) error {
	o := NewDBOrm(DBChessSysData)
	condition := fmt.Sprintf(`id = %v`, id)
	values, sql := UpdateSql(TableAreaGameList, data, condition)
	_, err := o.Raw(sql, values).Exec()
	return err
}

//删除区域游戏
func DeleteAreaGame(id int64, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChessSysData)
	condition := fmt.Sprintf("id=%v", id)
	sql := DeleteSql(TableAreaGameList, condition)
	result, err := o.Raw(sql).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

//删除区域游戏
func DeleteAreaGameByCityCode(gameId, cityCode interface{}, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChessSysData)
	condition := fmt.Sprintf("game_id = %v AND city_code = %v", gameId, cityCode)
	sql := DeleteSql(TableAreaGameList, condition)
	result, err := o.Raw(sql).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

//搜索游戏是否已经存在在区域游戏列表里
func IsAreaGameExist(gameId, cityCode int64) bool {
	o := NewDBOrm(DBChessSysData)
	condition := fmt.Sprintf("game_id = %v AND city_code = %v", gameId, cityCode)
	sql := fmt.Sprintf(`SELECT id FROM %v WHERE %v LIMIT 1`, TableAreaGameList, condition)
	var id int64
	o.Raw(sql).QueryRow(&id)
	if id != 0 {
		return true
	}
	return false
}

//获取区域游戏列表
func ListAreaGame(p ListParams, cityCode int64, multiOrm ...orm.Ormer) ([]orm.Params, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	sql := fmt.Sprintf(`
		SELECT
			a.id,a.position,a.game_id,
			g.name,g.game_type
		FROM
			%v AS a
		LEFT JOIN
			%v AS g
		ON a.game_id = g.game_id
		WHERE
			city_code = %v
		ORDER BY
			a.position
		LIMIT ?,?
		`, TableAreaGameList, TableGameList, cityCode)
	o := NewOrm(multiOrm, DBChessSysData)
	var gl []orm.Params
	_, err := o.Raw(sql, limit, p.PageSize).Values(&gl)
	if CheckError(err) {
		return nil, 0, err
	}
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v WHERE city_code = %v`,
		TableAreaGameList, cityCode)
	var count int64
	err = o.Raw(sql).QueryRow(&count)
	if CheckError(err) {
		return nil, 0, err
	}
	return gl, count, nil
}

//添加游戏
func NewGame(data map[string]interface{}, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChessSysData)
	values, sql := InsertSql(TableGameList, data)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	MakeGameConfigFile(o)
	return result.LastInsertId()
}

//更新游戏
func UpdateGame(data map[string]interface{}, gameId int64) (int64, error) {
	condition := fmt.Sprintf(`game_id = %v`, gameId)
	values, sql := UpdateSql(TableGameList, data, condition)
	o := NewDBOrm(DBChessSysData)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	MakeGameConfigFile(o)
	return result.RowsAffected()
}

//删除游戏
func DeleteGame(gameId int64, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChessSysData)
	condition := fmt.Sprintf("game_id = %v", gameId)
	sql := DeleteSql(TableGameList, condition)
	result, err := o.Raw(sql).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

func IsGameExistByGameId(gameId int64) (int64, bool) {
	sql := fmt.Sprintf(`
		SELECT
			game_id
		FROM
			%v
		WHERE
			game_id = %v
	`, TableGameList, gameId)
	var gid int64
	o := NewDBOrm(DBChessSysData)
	err := o.Raw(sql).QueryRow(&gid)
	if CheckNoExist(err) {
		return gid, false
	}
	return gid, true
}

func GameInfoById(gameId int64, multiOrm ...orm.Ormer) (*GameList, error) {
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			game_id = %v
	`, TableGameList, gameId)
	var gl GameList
	o := NewOrm(multiOrm, DBChessSysData)
	err := o.Raw(sql).QueryRow(&gl)
	if CheckNoExist(err) {
		return nil, nil
	}
	return &gl, err
}

//获取游戏列表
func ListGame(p ListParams, typ int64, multiOrm ...orm.Ormer) ([]*GameList, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	where := " is_delete = 0"
	if typ != 0 {
		where = where + fmt.Sprintf(` AND type = %v`, typ)
	}
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			%v
		LIMIT ?,?
		`, TableGameList, where)
	o := NewOrm(multiOrm, DBChessSysData)
	var gl []*GameList
	_, err := o.Raw(sql, limit, p.PageSize).QueryRows(&gl)
	if CheckError(err) {
		return gl, 0, err
	}
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v WHERE %v`,
		TableGameList, where)
	var count int64
	err = o.Raw(sql).QueryRow(&count)
	if CheckError(err) {
		return gl, 0, err
	}
	return gl, count, nil
}
